package edu.hubu.javaweb01demo.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JdbcUtil {//工具类抽取数据库配置文件内容
	private static String driverClass = "com.mysql.cj.jdbc.Driver";
	private static String url = "jdbc:mysql://localhost:3306/test01?serverTimezone=GMT&useUnicode=true&characterEncoding=utf-8&useSSL=false";
	private static String user = "root";
	private static String password = "339396";

	static {
		InputStream is = JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties");
		Properties prop = new Properties();//处理属性文件的工具类
		try {
			prop.load(is);
			driverClass = prop.getProperty("mysql.driverClass");
			url = prop.getProperty("mysql.url");
			user = prop.getProperty("mysql.name");
			password = prop.getProperty("mysql.pwd");
			Class.forName(driverClass);
		} catch (IOException | ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static Connection getConnection() {
		Connection conn = null;
		try {
			return DriverManager.getConnection(url, user, password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	public static void release(Connection conn, Statement ps, ResultSet res) {
		try {
			if(res!=null){
				res.close();
				res =null;
			}
			if (ps != null) {
				ps.close();
				ps = null;//垃圾回收，上！
			}
			if (conn != null) {
				conn.close();
				conn = null;//?
			}
		}catch (SQLException e) {
			e.printStackTrace();
		}

	}
	public static void main(String[] args) {
		System.out.println(driverClass);
	}
}
